﻿using System;
using System.Collections;
using System.Collections.Generic;
using AudioStudio;
using com.youzu.warh.protocol;
using LogSystem;
using UnityEngine;
using WorldMapBase.Layer;
using Yoozoo.Gameplay.Liberty.PlayerController;
using Yoozoo.Gameplay.Liberty.World;
using Yoozoo.Mars.Got;
using Random = UnityEngine.Random;

namespace Yoozoo.Gameplay.Liberty.AI
{
    /// <summary>
    /// 武器绑定骨骼点
    /// </summary>
    public enum WeaponBindBoneType
    {
        // 无
        None = 0,
        // 右手
        RightHand = 1,
        // 左手
        LeftHand = 2,
    }

    /// <summary>
    /// 武器IK类型
    /// </summary>
    public enum WeaponIKType
    {
        // 无
        None = 0,
        // 手枪瞄准IK
        HandGunAimIK = 1,
    }
    
    public class AnimationAgent : MonoBehaviour
    {
        // 近战长位移阈值
        public static readonly float BoxingMoveVeryLongThreshold = 1.52f;
        // 近战中位移阈值
        public static readonly float BoxingMoveLongThreshold = 1.51f;
        // 近战短位移阈值
        public static readonly float BoxingMoveShortThreshold = 1.5f;

        /// <summary>
        /// 近战位移类型枚举
        /// </summary>
        public enum BoxingMoveType
        {
            // 没有位移
            None = 0,
            // 短位移
            Short,
            // 中位移
            Long,
            // 长位移
            VeryLong,
        }

        /// <summary>
        /// 近战位移类型对应动画状态名称
        /// </summary>
        private static readonly Dictionary<BoxingMoveType, string> _BoxingMoveStateNames = new Dictionary<BoxingMoveType, string>
        {
            { BoxingMoveType.Short, "BoxingMoveShort" },
            { BoxingMoveType.Long , "BoxingMoveLong" },
            { BoxingMoveType.VeryLong , "BoxingMoveVeryLong" },
        };

        [SerializeField]
        private AIEntity _aiEntity;
        [SerializeField]
        private AIData _aiData;

        [SerializeField]
        private Animator _animator;

        // IK管理
        [SerializeField]
        private IKAgent _IKAgent;
        public IKAgent IKAgent => _IKAgent;
        
        // 禁止操作时间戳 (大于0时生效)
        public float _disableOperateTimestamp;

        // 上次战斗状态时间
        private float _lastBattleTimestamp;
        // 战斗状态保持时长
        [SerializeField]
        private float _keepBattleDuration = 5f;
        // 被击倒保持的时长
        private float _keepOnHitDownDuration => Random.Range(2f, 3f);
        
        // 击倒起身的时间戳
        public float onHitGetUpTimestamp => _onHitGetUpTimestamp;
        private float _onHitGetUpTimestamp;
        
        // 本次连击状态
        private AnimationState _currentAnimationState;
        public AnimationState currentAnimationState => _currentAnimationState;
        // 下次连击的时间戳 (可接收下次连招的开始时间戳)
        private float _nextComboTimestamp;
        public float nextComboTimestamp => _nextComboTimestamp;
        // 下次连击带缓冲时长的时间戳 (可接收下次连招的结束时间戳)
        private float _nextComboBufferTimestamp;
        public float nextComboBufferTimestamp => _nextComboBufferTimestamp;
        // 近战位移回调
        private Action _boxingMoveCallback = null;
        
        // 近战AOECD完成的时间戳
        public float boxingAOEColdFinishTimestamp => _boxingAOEColdFinishTimestamp;
        private float _boxingAOEColdFinishTimestamp;
        // 近战AOE总CD
        public float boxingAOEColdDuration => _boxingAOEColdDuration;
        private float _boxingAOEColdDuration;
        
        // 装弹CD完成的时间戳
        public float reloadFinishTimestamp => _reloadFinishTimestamp;
        private float _reloadFinishTimestamp;
        
        // 武器开火位置脏标记
        private bool _rangeWeaponFirePositionDirty = false;
        
        // OverrideController（用于动态替换动画）
        private AnimatorOverrideController _overrideController;
        // 当前使用的占位符
        private int _currentPlaceHolderIndex = -1;
        // 占位符的动画的名称
        private static string[] _placeHolderClipNames = new[] { "PlaceHolder_1", "PlaceHolder_2" };
        // 占位符的状态名称
        private static string[] _placeHolderStateNames = new[] { "PLACEHOLDER_1", "PLACEHOLDER_2" }; 
        
        // 投掷手雷参数
        // 投掷手雷的起始点
        Vector3 _ThrowEndPoint = Vector3.zero;
        // 投掷手雷的力
        float _ThrowMaxHeight = 0;
        
        // RPG瞄准方向
        Vector3 _FireRPGDirection = Vector3.zero;
        // RPG生成位置
        Vector3 _FireRPGPosition = Vector3.zero;
        
        void Awake()
        {
            if(_animator== null)
            {
                _animator = gameObject.GetComponent<Animator>();
            }
            if (_IKAgent == null)
            {
                _IKAgent = gameObject.GetOrAddComponent<IKAgent>();
            }
            _IKAgent.SetAfterHandGunIKUpdate(AfterHandGunIKUpdate);
        }

        public void InitData(AIEntity aiEntity)
        {
            _aiEntity = aiEntity;
            _aiData = aiEntity.data;
            _IKAgent.InitData(aiEntity);
        }

        void OnDestroy()
        {
            _aiEntity = null;
            _aiData = null;
        }

        /// <summary>
        /// 设置状态保持时长
        /// </summary>
        /// <param name="keepDuration"></param>
        public void SetKeepBattleDuration(float keepDuration)
        {
            _keepBattleDuration = keepDuration;
        }
        
        /// <summary>
        /// 设置当前角色动画状态
        /// </summary>
        /// <param name="animationState"></param>
        public void OnAnimationEnter(int layerIndex, AnimationState animationState, int fullPathHash)
        {
            // int entityId = _aiEntity != null ? _aiEntity.id : 0;
            // Debug.LogError(LogModule.LibertySceneBattle,$"==={entityId}==OnAnimationEnter===Layer:{layerIndex}===={animationState}=====hash:{fullPathHash}==next:{_animator.GetNextAnimatorStateInfo(0).fullPathHash}===");

            {
                // 记录该层的动画状态
                _aiData.allAnimationStates[layerIndex] = animationState;
                
                if (layerIndex == 0)
                {
                    _aiData.baseAnimationState = animationState;

                    // int entityId = _aiEntity != null ? _aiEntity.id : 0;
                    // Debug.LogError(LogModule.LibertySceneBattle,$"==={entityId}==OnAnimationEnter===BaseLayer===={animationState}====={duration}s===");
                }
                else
                {
                    // Debug.LogError(LogModule.LibertySceneBattle,$"=====SetAnimationState===UpperLayer===={animationState}====={duration}s===");
                }
            }
            
            // 需要处理的特殊状态
            switch (animationState)
            {
                case AnimationState.DodgeSprint:
                case AnimationState.Dodge:
                    {
                        _aiData.animInvincible = true;
                    }
                    break;
                
                case AnimationState.BoxingCombo1:
                case AnimationState.BoxingCombo2:
                case AnimationState.BoxingCombo3:
                case AnimationState.BoxingAttackKick:
                case AnimationState.BoxingAttackDown:
                case AnimationState.HandGunBoxing:
                case AnimationState.BoxingRunAttack:
                    {
                        // 近战状态 (记录时间，用于连招)
                        _currentAnimationState = animationState;
                        
                        PbBoxingDataConfig boxingDataConfig = LibertyConfigManager.GetInstance().GetBoxingDataConfig(_aiData.uniqueId, animationState);
                        _nextComboTimestamp = Time.time + boxingDataConfig.back_rock;
                        _nextComboBufferTimestamp = _nextComboTimestamp + boxingDataConfig.combo_response_time;
                        // Debug.LogError(LogModule.LibertySceneBattle, $"=====BoxingTimestamp===={animationState}====={_nextComboTimestamp}s===");
                    }
                    break;
                
                case AnimationState.BoxingAOE:
                    {
                        // 近战状态 (记录时间，用于连招)
                        _currentAnimationState = animationState;
                            
                        PbBoxingDataConfig boxingDataConfig = LibertyConfigManager.GetInstance().GetBoxingDataConfig(_aiData.uniqueId, animationState);
                        _nextComboTimestamp = Time.time + boxingDataConfig.back_rock;
                        _nextComboBufferTimestamp = _nextComboTimestamp + boxingDataConfig.combo_response_time;
                        // Debug.LogError(LogModule.LibertySceneBattle, $"=====BoxingTimestamp===={_aiData.boxingAnimationState}====={_aiData.boxingTimestamp}s===");

                        if (Time.time >= _boxingAOEColdFinishTimestamp) // 处理多段动画状态机重复设置cd的bug
                        {
                            _boxingAOEColdDuration = boxingDataConfig.CD;
                            // 近战AOECD完成时间戳
                            _boxingAOEColdFinishTimestamp = Time.time + boxingDataConfig.CD;
                        }
                    }
                    break;

                case AnimationState.OnHitDown:
                    {
                        // 受击
                        // 起身动作时间戳
                        _onHitGetUpTimestamp = Time.time + _keepOnHitDownDuration;
                    }
                    break;
                
                case AnimationState.HandGunShoot:
                case AnimationState.AutoRifleShoot:
                case AnimationState.BombThrow:
                case AnimationState.RPGShoot:
                    {
                        // 枪械射击 (记录时间，用于cd)
                        PbWeaponDataConfig weaponDataConfig = _aiEntity.WeaponSystem.HandleWeapon.Config;
                        _nextComboTimestamp = Time.time + weaponDataConfig.shooting_cd;
                    }
                    break;

                case AnimationState.HandGunReload:
                case AnimationState.AutoRifleReload:
                case AnimationState.BombReload:
                case AnimationState.RPGReload:
                    {
                        // 枪械重新装弹 (记录时间，用于cd)
                        PbWeaponDataConfig weaponDataConfig = _aiEntity.WeaponSystem.HandleWeapon.Config;
                        _nextComboTimestamp = Time.time + weaponDataConfig.reload_CD;

                        // 装弹CD完成时间戳
                        _reloadFinishTimestamp = _nextComboTimestamp;
                    }
                    break;
            }
            
        }

        public void OnAnimationUpdate(int layerIndex, AnimationState animationState, int fullPathHash)
        {
            // int entityId = _aiEntity != null ? _aiEntity.id : 0;
            // Debug.LogError(LogModule.LibertySceneBattle,$"=={entityId}===OnAnimationUpdate===Layer:{layerIndex}===={animationState}====hash:{fullPathHash}===next:{_animator.GetNextAnimatorStateInfo(0).fullPathHash}====");

            // if (layerIndex == 0)
            {
                // 偶尔会有OnAnimationEnter没有执行的情况，这里获取一下下一个动画状态，如果和当前状态不一致，自己手动触发一下OnAnimationEnter
                // 例如: GTA世界刚进入时，npc会在AnimationState.Idle状态，但实际是AnimationState.Locomotion状态
                int nextFullPathHash = _animator.GetNextAnimatorStateInfo(layerIndex).fullPathHash;
                if (fullPathHash==nextFullPathHash && _aiData.baseAnimationState!=animationState)
                {
                    OnAnimationEnter(layerIndex, animationState, fullPathHash);
                }
            }


        }

        public void OnAnimationExit(int layerIndex, AnimationState animationState, int fullPathHash)
        {
            // int entityId = _aiEntity != null ? _aiEntity.id : 0;
            // Debug.LogError(LogModule.LibertySceneBattle,$"=={entityId}===OnAnimationExit===Layer:{layerIndex}===={animationState}========");

            switch (animationState)
            {
                case AnimationState.DodgeSprint:
                case AnimationState.Dodge:
                    {
                        _aiData.animInvincible = false;
                    }
                    break;
            }

            if (_aiEntity.id == LibertyAIUtils.PLAYER_AI_ID)
            {
                // 玩家
                switch (animationState)
                {
                    case AnimationState.HandGunShoot:
                    case AnimationState.AutoRifleShoot:
                    case AnimationState.RPGShoot:
                        {
                            // 没有子弹了，切换武器
                            BattleAgent battleAgent = _aiEntity.battleAgent;
                            if (battleAgent.GetRestBulletCount() <= 0 && !battleAgent.IsRestBulletCountEnough())
                            {
                                int weaponId = _aiEntity.WeaponSystem.GetCanUseFirstPriorityWeaponId(false);
                                if (weaponId > 0)
                                {
                                    LibertyAIUtils.PlayerController.EuipAndSwitchWeapon(weaponId);
                                }
                            }
                        }
                        break;
                
                    case AnimationState.BombThrow:
                        {
                            // 没有子弹了，切换武器
                            BattleAgent battleAgent = _aiEntity.battleAgent;
                            if (battleAgent.GetRestBulletCount() <= 0 && !battleAgent.IsRestBulletCountEnough())
                            {
                                int weaponId = _aiEntity.WeaponSystem.GetCanUseFirstPriorityWeaponId(true);
                                if (weaponId > 0)
                                {
                                    LibertyAIUtils.PlayerController.EuipAndSwitchWeapon(weaponId);
                                }
                                else
                                {
                                    weaponId = _aiEntity.WeaponSystem.GetCanUseFirstPriorityWeaponId(false);
                                    if (weaponId > 0)
                                    {
                                        LibertyAIUtils.PlayerController.EuipAndSwitchWeapon(weaponId);
                                    }
                                }
                            }
                        }
                        break;
                }
            }
            
        }

        
        /// <summary>
        /// 获取当前角色动画状态
        /// </summary>
        /// <returns></returns>
        public AnimationState GetAnimationState()
        {
            return _aiData.animationState;
        }

        /// <summary>
        /// 更新最后战斗状态的时间戳
        /// </summary>
        public void UpdateLastBattleTimestamp()
        {
            _lastBattleTimestamp = Time.time;
        }

#region 播放动画

        /// <summary>
        /// 是否可以进行下个动作 (战斗动作必须等上个动作做完，才可以接下一步)
        /// </summary>
        /// <returns></returns>
        public bool CanDoNextAnimation()
        {
            // 如果是玩家
            if (_aiData.uniqueId == LibertyAIUtils.PLAYER_AI_ID)
            {
                // 玩家进入车的状态
                if (LibertyAIUtils.PlayerController.vehicleBehaviourMgr.enterExitCarState != EnterExitCarState.None)
                {
                    return false;
                }
            }
            
            // 被撞 或 上下车中
            switch (_aiData.npcStatus)
            {
                case NpcStatus.VehicleBarge:
                case NpcStatus.EnterCar:
                case NpcStatus.InCar:
                case NpcStatus.ExitCar:
                case NpcStatus.ExpelCar:
                {
                    return false;
                }
            }
            
            // 死亡
            if (!_aiData.isAlive)
            {
                return false;
            }

            // 检测当前动画状态
            switch (_aiData.animationState)
            {
                // case AnimationState.OnHit:
                case AnimationState.OnHitDown:
                case AnimationState.OnHitGetUp:
                    {
                        return false;
                    }
            }

            // 检测当前战斗状态
            switch (_aiData.battleState)
            {
                case BattleState.None:
                    {
                        // 空闲
                        return nextComboTimestamp < Time.time;
                    }
            

                case BattleState.Boxing:
                    {
                        // 近战
                        return nextComboTimestamp < Time.time;
                    }
         

                case BattleState.HandGun:
                case BattleState.AutoRifle:
                case BattleState.Bomb:
                case BattleState.RPG:
                    {
                        // 自动枪械
                        return nextComboTimestamp < Time.time;
                    }

            }

            return true;
        }

        /// <summary>
        /// 设置动画是否禁用其他输入状态（用于表演类型的动画）
        /// </summary>
        /// <param name="enable"></param>
        public void SetDisableOperate(bool enable, float disableDuration = 0,bool force = false)
        {
            if (_aiData == null)
            {
                return;
            }

            if (!enable && !_aiData.isAlive && !force) //死亡的enable == false的不作用
            {
                return;
            }
            // UnityEngine.Debug.LogError($"SetDisableOperate {_aiData.uniqueId} {enable}");
            
            _aiEntity.viewer.animator.SetBool("DisableOperate", enable);
            if (enable && disableDuration > 0)
            {
                _disableOperateTimestamp = Time.time + disableDuration;
            }
            else
            {
                _disableOperateTimestamp = -1;
            }
        }
        
        /// <summary>
        /// 设置瞄准状态
        /// </summary>
        /// <param name="aiming"></param>
        public void EnterAimingState(AimingState aimingState)
        {
            if (_aiData == null)
            {
                return;
            }
            
            if (_aiData.aimingState == aimingState)
            {
                return;
            }
            
            // 关闭瞄准IK
            _IKAgent.SetHandGunEnabled(false); 

            _aiData.aimingState = aimingState;
            switch (aimingState)
            {
                case AimingState.None:
                    {
                        _aiEntity.viewer.animator.SetBool("Aiming", false);
                    }
                    break;

                case AimingState.NormalAim:
                    {
                        _aiEntity.viewer.animator.SetBool("Aiming", true);
                        
                        // 更新瞄准完成的时间戳
                        if (_aiData.uniqueId == LibertyAIUtils.PLAYER_AI_ID)
                        {
                            _nextComboTimestamp = Time.time + 0.3f;
                        }
                        
                        // 瞄准IK淡入
                        _IKAgent.HandGunAimIKFadeIn(); 
                    }
                    break;

                case AimingState.ReloadAim:
                    {
                        // 瞄准状态时换弹，播放换弹idle，并关闭ik
                        _aiEntity.viewer.animator.SetBool("Aiming", true);
                        
                        // 瞄准IK淡出
                        // _IKAgent.HandGunAimIKFadeOut();
                        _IKAgent.HandGunAimIKFadeIn();
                    }
                    break;

                case AimingState.Grenade:
                    {
                        _aiEntity.viewer.animator.SetBool("Aiming", true);
                    }
                    break;
            }
            
            // 更新最后战斗状态的时间戳
            UpdateLastBattleTimestamp();
        }

        /// <summary>
        /// 切换模式
        /// </summary>
        /// <param name="battleState"></param>
        public void EnterBattleState(BattleState battleState)
        {
            if (_aiData == null)
            {
                return;
            }
            
            if (_aiData.battleState == battleState)
            {
                return;
            }
            // 进入新模式
            _aiData.battleState = battleState;
            _aiEntity.viewer.animator.SetInteger("SetEquipId", (int)battleState);
            switch (battleState)
            {
                case BattleState.None:
                    {
                        EnterAimingState(AimingState.None);
                    }
                    break;
                
                default:
                    {
                        // 替换装备
                        PbWeaponDataConfig weaponDataConfig = _aiEntity.WeaponSystem.HandleWeapon.Config;
                        if (weaponDataConfig != null)
                        {
                            _IKAgent.SetHandGunPoleTargetPosition(new Vector3(weaponDataConfig.ik_pole_position[0], weaponDataConfig.ik_pole_position[1], weaponDataConfig.ik_pole_position[2]));
                            _IKAgent.SetHandGunPoleWeight(weaponDataConfig.ik_pole_weight);
                        }
                    }
                    break;
            }
        }

        /// <summary>
        /// 重置Agent （用于玩家重生）
        /// </summary>
        public void ResetAnimationAgent()
        {
            // EnterBattleState(BattleState.None);
            EnterAimingState(AimingState.None);
            SetDisableOperate(false);

            PlayIdleAnimation();
        }

        /// <summary>
        /// 近战向下攻击
        /// </summary>
        public void PlayBoxingAttackDown(BoxingMoveType moveType = BoxingMoveType.None)
        {
            switch (moveType)
            {
                case BoxingMoveType.None:
                    {
                        playBoxingAttackDown();
                    }
                    break;
                
                default:
                    {
                        // _aiData.baseAnimationState = AnimationState.BoxingMove;   // 手动设置，提前锁死移动摇杆
                        // BaseLayer
                        _aiEntity.viewer.animator.CrossFadeInFixedTime(_BoxingMoveStateNames[moveType], 0.2f, 0);
                        // BoxingMove回调
                        _boxingMoveCallback = playBoxingAttackDown;
                    }
                    break;
            }
        }

        void playBoxingAttackDown()
        {
            // _aiData.baseAnimationState = AnimationState.BoxingAttackDown;   // 手动设置，提前锁死移动摇杆
            // BaseLayer
            _aiEntity.viewer.animator.CrossFadeInFixedTime("BoxingAttackDown", 0.2f, 0);
        }

        /// <summary>
        /// 近战踢腿
        /// </summary>
        public void PlayBoxingAttackKick(BoxingMoveType moveType = BoxingMoveType.None)
        {
            switch (moveType)
            {
                case BoxingMoveType.None:
                    {
                        playBoxingAttackKick();
                    }
                    break;
                
                default:
                    {
                        // _aiData.baseAnimationState = AnimationState.BoxingMove;   // 手动设置，提前锁死移动摇杆
                        // BaseLayer
                        _aiEntity.viewer.animator.CrossFadeInFixedTime(_BoxingMoveStateNames[moveType], 0.2f, 0);
                        // BoxingMove回调
                        _boxingMoveCallback = playBoxingAttackKick;
                    }
                    break;
            }
        }
        
        void playBoxingAttackKick()
        {
            // _aiData.baseAnimationState = AnimationState.BoxingAttackKick;   // 手动设置，提前锁死移动摇杆
            // // BaseLayer
            // _aiEntity.viewer.animator.CrossFadeInFixedTime("BoxingAttackKick", 0.2f, 0);
            _aiEntity.viewer.animator.SetTrigger("boxing_3");
        }
        
        /// <summary>
        /// 近战连招1
        /// </summary>
        public void PlayBoxingCombo1(BoxingMoveType moveType = BoxingMoveType.None)
        {
            switch (moveType)
            {
                case BoxingMoveType.None:
                    {
                        playBoxingCombo1();
                    }
                    break;
                
                default:
                    {
                        // _aiData.baseAnimationState = AnimationState.BoxingMove;   // 手动设置，提前锁死移动摇杆
                        // BaseLayer
                        _aiEntity.viewer.animator.CrossFadeInFixedTime(_BoxingMoveStateNames[moveType], 0.2f, 0);
                        // BoxingMove回调
                        _boxingMoveCallback = playBoxingCombo1;
                    }
                    break;
            }
        }
        
        public void playBoxingCombo1()
        {
            // _aiData.baseAnimationState = AnimationState.BoxingCombo1;   // 手动设置，提前锁死移动摇杆
            // // BaseLayer
            // _aiEntity.viewer.animator.CrossFadeInFixedTime("BoxingCombo1", 0.2f, 0);
            _aiEntity.viewer.animator.SetTrigger("Fire");

            _nextComboTimestamp = Time.time + 1;// 防止连续触发trigger
        }
        
        /// <summary>
        /// 近战连招2
        /// </summary>
        public void PlayBoxingCombo2(BoxingMoveType moveType = BoxingMoveType.None)
        {
            switch (moveType)
            {
                case BoxingMoveType.None:
                    {
                        playBoxingCombo2();
                    }
                    break;
                
                default:
                    {
                        // _aiData.baseAnimationState = AnimationState.BoxingMove;   // 手动设置，提前锁死移动摇杆
                        // BaseLayer
                        _aiEntity.viewer.animator.CrossFadeInFixedTime(_BoxingMoveStateNames[moveType], 0.2f, 0);
                        // BoxingMove回调
                        _boxingMoveCallback = playBoxingCombo2;
                    }
                    break;
            }
        }
        
        void playBoxingCombo2()
        {
            // _aiData.baseAnimationState = AnimationState.BoxingCombo2;   // 手动设置，提前锁死移动摇杆
            // // BaseLayer
            // _aiEntity.viewer.animator.CrossFadeInFixedTime("BoxingCombo2", 0.2f, 0);
            _aiEntity.viewer.animator.SetTrigger("boxing_2");
            
            _nextComboTimestamp = Time.time + 1;// 防止连续触发trigger
        }
        
        /// <summary>
        /// 近战连招3
        /// </summary>
        public void PlayBoxingCombo3(BoxingMoveType moveType = BoxingMoveType.None)
        {
            switch (moveType)
            {
                case BoxingMoveType.None:
                    {
                        playBoxingCombo3();
                    }
                    break;
                
                default:
                    {
                        // _aiData.baseAnimationState = AnimationState.BoxingMove;   // 手动设置，提前锁死移动摇杆
                        // BaseLayer
                        _aiEntity.viewer.animator.CrossFadeInFixedTime(_BoxingMoveStateNames[moveType], 0.2f, 0);
                        // BoxingMove回调
                        _boxingMoveCallback = playBoxingCombo3;
                    }
                    break;
            }
        }
        
        void playBoxingCombo3()
        {            
            // _aiData.baseAnimationState = AnimationState.BoxingCombo3;   // 手动设置，提前锁死移动摇杆
            // // BaseLayer
            // _aiEntity.viewer.animator.CrossFadeInFixedTime("BoxingCombo3", 0.2f, 0);
            _aiEntity.viewer.animator.SetTrigger("boxing_3");
            
            _nextComboTimestamp = Time.time + 1;// 防止连续触发trigger
        }
        
        /// <summary>
        /// 近战跑动攻击
        /// </summary>
        public void PlayBoxingRunAttack(BoxingMoveType moveType = BoxingMoveType.None)
        {
            switch (moveType)
            {
                case BoxingMoveType.None:
                    {
                        playBoxingRunAttack();
                    }
                    break;
                
                default:
                    {
                        // _aiData.baseAnimationState = AnimationState.BoxingMove;   // 手动设置，提前锁死移动摇杆
                        // BaseLayer
                        _aiEntity.viewer.animator.CrossFadeInFixedTime(_BoxingMoveStateNames[moveType], 0.2f, 0);
                        // BoxingMove回调
                        _boxingMoveCallback = playBoxingRunAttack;
                    }
                    break;
            }
        }

        void playBoxingRunAttack()
        {
            // _aiData.baseAnimationState = AnimationState.BoxingRunAttack;   // 手动设置，提前锁死移动摇杆
            // BaseLayer
            _aiEntity.viewer.animator.CrossFadeInFixedTime("BoxingRunAttack", 0.2f, 0);
        }
        
        /// <summary>
        /// 近战AOE攻击
        /// </summary>
        public void PlayBoxingAOE(BoxingMoveType moveType = BoxingMoveType.None)
        {
            switch (moveType)
            {
                case BoxingMoveType.None:
                    {
                        playBoxingAOE();
                    }
                    break;
                
                default:
                    {
                        // _aiData.baseAnimationState = AnimationState.BoxingMove;   // 手动设置，提前锁死移动摇杆
                        // BaseLayer
                        _aiEntity.viewer.animator.CrossFadeInFixedTime(_BoxingMoveStateNames[moveType], 0.2f, 0);
                        // BoxingMove回调
                        _boxingMoveCallback = playBoxingAOE;
                    }
                    break;
            }
        }

        void playBoxingAOE()
        {
            // _aiData.baseAnimationState = AnimationState.BoxingAOE;   // 手动设置，提前锁死移动摇杆
            // BaseLayer
            _aiEntity.viewer.animator.CrossFadeInFixedTime("BoxingAOE", 0.2f, 0);
        }

        /// <summary>
        /// 受击左
        /// </summary>
        public void PlayHurtLeft()
        {
            // // BaseLayer
            // _aiEntity.viewer.animator.CrossFadeInFixedTime("HurtLeft", 0.2f, 0);
            _aiEntity.viewer.animator.SetTrigger("HurtLeft");
        }
        
        /// <summary>
        /// 受击右
        /// </summary>
        public void PlayHurtRight()
        {
            // // BaseLayer
            // _aiEntity.viewer.animator.CrossFadeInFixedTime("HurtRight", 0.2f, 0);
            _aiEntity.viewer.animator.SetTrigger("HurtRight");
        }
        
        /// <summary>
        /// 受击踢腿
        /// </summary>
        public void PlayHurtKick()
        {
            // // BaseLayer
            // _aiEntity.viewer.animator.CrossFadeInFixedTime("HurtKick", 0.2f, 0);
            _aiEntity.viewer.animator.SetTrigger("HurtDown");
        }

        /// <summary>
        /// 受击后退
        /// </summary>
        public void PlayHurtBack()
        {
            // // BaseLayer
            // _aiEntity.viewer.animator.CrossFadeInFixedTime("HurtBack", 0.2f, 0);
            _aiEntity.viewer.animator.SetTrigger("HurtBack");
        }
        
        /// <summary>
        /// 受击后退并倒地
        /// </summary>
        public void PlayHurtBackDown()
        {
            // BaseLayer
            _aiEntity.viewer.animator.CrossFadeInFixedTime("HurtBackDown", 0.2f, 0);
        }
        
        /// <summary>
        /// 受击倒地后起身
        /// </summary>
        public void PlayHurtGetUp()
        {
            // BaseLayer
            _aiEntity.viewer.animator.CrossFadeInFixedTime("HurtGetUp", 0.2f, 0);
        }

        /// <summary>
        /// 射击
        /// </summary>
        public void PlayGunFire()
        {
            // // UpperBody
            // _aiEntity.viewer.animator.CrossFadeInFixedTime("HandGunAttack", 0.2f, 1);
            _aiEntity.viewer.animator.SetTrigger("Fire");

            // 记录武器开火前位置
            SetRangeWeaponFirePositionDirty();
        }

        /// <summary>
        /// 手枪近战攻击
        /// </summary>
        public void PlayHandGunBoxing()
        {
            // BaseBody
            _aiEntity.viewer.animator.CrossFadeInFixedTime("HandGunBoxing", 0.2f, 0);
        }
        
        /// <summary>
        /// 装弹
        /// </summary>
        public void PlayHandGunReload()
        {
            // UpperBody
            _aiEntity.viewer.animator.SetBool("Reload", true);

            if (_aiData.aimingState != AimingState.None)
            {
                EnterAimingState(AimingState.ReloadAim);
                _reloadFinishTimestamp = -1;   // 避免立即切换完成
                _nextComboTimestamp = Time.time + 1;    // 防止连续触发trigger
            }
        }

        /// <summary>
        /// 手枪受击左
        /// </summary>
        public void PlayHandGunHurtLeft()
        {
            // BaseLayer
            _aiEntity.viewer.animator.CrossFadeInFixedTime("HandGunHurtLeft", 0.2f, 0);
        }
        
        /// <summary>
        /// 手枪受击中间
        /// </summary>
        public void PlayHandGunHurtMiddle()
        {
            // BaseLayer
            _aiEntity.viewer.animator.CrossFadeInFixedTime("HandGunHurtMid", 0.2f, 0);
        }
        
        /// <summary>
        /// 手枪受击右
        /// </summary>
        public void PlayHandGunHurtRight()
        {
            // BaseLayer
            _aiEntity.viewer.animator.CrossFadeInFixedTime("HandGunHurtRight", 0.2f, 0);
        }
        
        /// <summary>
        /// 投掷手雷
        /// </summary>
        /// <param name="endPoint"></param>
        /// <param name="maxHeight"></param>
        public void PlayThrowGrenade(Vector3 endPoint, float maxHeight)
        {
            _aiEntity.viewer.animator.SetTrigger("Fire");

            _ThrowEndPoint = endPoint;
            _ThrowMaxHeight = maxHeight;
        }
        
        /// <summary>
        /// 手雷装弹
        /// </summary>
        public void PlayGrenadeReload()
        {
            // UpperBody
            _aiEntity.viewer.animator.SetBool("Reload", true);

            if (_aiData.aimingState != AimingState.None)
            {
                EnterAimingState(AimingState.None);
                _reloadFinishTimestamp = -1;   // 避免立即切换完成
            }
        }

        /// <summary>
        /// 火箭筒发射
        /// </summary>
        public void PlayRPGFire(Vector3 position, Vector3 direction)
        {
            _aiEntity.viewer.animator.SetTrigger("Fire");

            _FireRPGPosition = position;
            _FireRPGDirection = direction;
        }

        /// <summary>
        /// 火箭筒装弹
        /// </summary>
        public void PlayRPGReload()
        {
            _aiEntity.viewer.animator.SetBool("Reload", true);

            if (_aiData.aimingState != AimingState.None)
            {
                EnterAimingState(AimingState.ReloadAim);
                _reloadFinishTimestamp = -1;   // 避免立即切换完成
                _nextComboTimestamp = Time.time + 1;    // 防止连续触发trigger
            }
        }
        
        void SetRangeWeaponFirePositionDirty()
        {
            _rangeWeaponFirePositionDirty = true;
        }

        #endregion

#region 播放演示动画

        /// <summary>
        /// 动态播放街区事件动画 (传动画名，动画需要放在指定路径下：Assets/ResourcesAssets/Hanging/Animations/{0}.anim)
        /// </summary>
        /// <param name="animationName"></param>
        /// <param name="callback"></param>
        public void PlayStreetEventDynamicAnimation(string animationName, Action callback = null)
        {
            if (string.IsNullOrEmpty(animationName))
            {
                Debug.LogWarning(LogModule.LibertySceneBattle, "动态播放街区事件动画名称为空字符串");
                callback?.Invoke();
                return;
            }
            
            PlayDynamicAnimation(LibertyAnimationClipManager.GetInstance().ConvertStreetEventAnimationName2Path(animationName), callback);
        }

        /// <summary>
        /// 动态播放指定路径的动画 (需使用完整资源路径)
        /// </summary>
        /// <param name="path"></param>
        /// <param name="callback"></param>
        public void PlayDynamicAnimation(string path, Action callback = null)
        {
            // 移除计时器
            Timers.inst.Remove(delayResetDisableOperateFalse);

            Animator animator = _aiEntity.viewer.animator;
  
            if (_overrideController == null)
            {
                AnimatorOverrideController animatorOverrideController = animator.runtimeAnimatorController as AnimatorOverrideController;
                if (!animatorOverrideController)
                {
                    // 使用原始AnimatorController
                    _overrideController = new AnimatorOverrideController();
                    _overrideController.runtimeAnimatorController = animator.runtimeAnimatorController;
                    _aiEntity.viewer.animator.runtimeAnimatorController = _overrideController;
                }
                else
                {
                    // 使用AniamtorOverrideController
                    _overrideController = animatorOverrideController;
                }
            }

            LibertyAnimationClipManager.GetInstance().LoadAnimationClip(path, clip =>
            {
                // 获取空闲占位符
                _currentPlaceHolderIndex = (_currentPlaceHolderIndex + 1) % _placeHolderClipNames.Length;
                // 替换动画
                _overrideController[_placeHolderClipNames[_currentPlaceHolderIndex]] = clip;
                // 播放
                SetDisableOperate(true);
                _aiEntity.viewer.animator.CrossFadeInFixedTime(_placeHolderStateNames[_currentPlaceHolderIndex], 0.2f, 0);

                if (!clip.isLooping)
                {
                    Timers.inst.Add(clip.length, 1, delayResetDisableOperateFalse);
                }
                
                callback?.Invoke();
            });
        }

        // 延迟重置DisableOperate
        void delayResetDisableOperateFalse(object o)
        {
            if (!_aiEntity)
            {
                return;
            }

            if (_aiEntity.data.isAlive)
            {
                SetDisableOperate(false);
            }
        }

        /// <summary>
        /// 将角色状态切回到Idle
        /// </summary>
        public void PlayIdleAnimation()
        {
            _aiEntity.viewer.animator.CrossFadeInFixedTime("Idle", 0.2f, 0);
        }

#endregion
        
        
#region 动画事件回调

        /// <summary>
        /// 近战动画事件回调
        /// </summary>
        /// <param name="evt"></param>
        void OnBoxingEvent(AnimationEvent evt)
        {
            AnimationState animationState = (AnimationState)evt.intParameter;
            // switch (animationState)
            // {
            //     case AnimationState.BoxingCombo1:
            //         {
            //             UnityEngine.Debug.LogError($"OnBoxingEvent {AnimationState.BoxingCombo1}");   
            //         }
            //         break;
            //     case AnimationState.BoxingCombo2:
            //         {
            //             UnityEngine.Debug.LogError($"OnBoxingEvent {AnimationState.BoxingCombo2}");   
            //         }
            //         break;
            //     case AnimationState.BoxingCombo3:
            //         {
            //             UnityEngine.Debug.LogError($"OnBoxingEvent {AnimationState.BoxingCombo3}");   
            //         }
            //         break;
            //     case AnimationState.BoxingAttackKick:
            //         {
            //             UnityEngine.Debug.LogError($"OnBoxingEvent {AnimationState.BoxingAttackKick}");   
            //         }
            //         break;
            //     case AnimationState.BoxingAttackDown:
            //         {
            //             UnityEngine.Debug.LogError($"OnBoxingEvent {AnimationState.BoxingAttackDown}");   
            //         }
            //         break;
            // }

            if (animationState == AnimationState.BoxingMove)
            {
                // 位移处理
                _boxingMoveCallback?.Invoke();
                _boxingMoveCallback = null;
            }
            else
            {
                // 攻击处理
                _aiEntity.battleAgent.BoxingAttack(animationState);
            }
        }
        
        /// <summary>
        /// 手枪动画事件回调
        /// </summary>
        /// <param name="evt"></param>
        void OnHandGunEvent(AnimationEvent evt)
        {
            AnimationState animationState = (AnimationState)evt.intParameter;
            switch (animationState)
            {
                case AnimationState.HandGunShoot:
                case AnimationState.AutoRifleShoot:
                    {
                        // 攻击处理
                        _aiEntity.battleAgent.PhysicsRaycastGunAttack(animationState);  
                    }
                    break;

                case AnimationState.HandGunReloadStart:
                case AnimationState.AutoRifleReloadStart:
                    {
                        // 开始装弹
                        // 显示弹夹
                        _aiEntity.battleAgent.ShowMagazine();
                    }
                    break;
                
                case AnimationState.HandGunReload:
                case AnimationState.AutoRifleReload:
                    {
                        // 结束装弹
                        // 装弹处理
                        _aiEntity.battleAgent.GunReload();
                        // 隐藏弹夹
                        _aiEntity.battleAgent.HideMagazine();
                        // 手动结束换弹状态
                        _aiEntity.viewer.animator.SetBool("Reload", false);
                    }
                    break;
            }
        }

        /// <summary>
        /// 手雷动画事件回调
        /// </summary>
        /// <param name="evt"></param>
        void OnGrenadeEvent(AnimationEvent evt)
        {
            AnimationState animationState = (AnimationState)evt.intParameter;
            switch (animationState)
            {
                case AnimationState.BombThrow:
                    {
                        // 投掷处理
                        
                        // 计算移动偏移量
                        _aiEntity.battleAgent.ThrowGrenade(_ThrowEndPoint, _ThrowMaxHeight);
                        
                        // 隐藏手上的手雷
                        LibertyWeapon libertyWeapon = _aiEntity.weapon;
                        if (libertyWeapon)
                        {
                            libertyWeapon.OnWeaponFire();
                        }
                    }
                    break;
                
                case AnimationState.BombReload:
                    {
                        // 装弹处理
                        _aiEntity.battleAgent.GunReload();
                        // 手动结束换弹状态
                        _aiEntity.viewer.animator.SetBool("Reload", false);
                        
                        // 重新显示手上的手雷
                        LibertyWeapon libertyWeapon = _aiEntity.weapon;
                        if (libertyWeapon)
                        {
                            libertyWeapon.OnWeaponReload();
                        }
                    }
                    break;
            }
        }

        /// <summary>
        /// RPG动画事件回调
        /// </summary>
        /// <param name="evt"></param>
        void OnRPGEvent(AnimationEvent evt)
        {
            AnimationState animationState = (AnimationState)evt.intParameter;
            switch (animationState)
            {
                case AnimationState.RPGShoot:
                    {
                        // 发射RPG
                        _aiEntity.battleAgent.FireRPG(_FireRPGPosition, _FireRPGDirection);
                        
                        // 隐藏武器上需要隐藏的部分
                        LibertyWeapon libertyWeapon = _aiEntity.weapon;
                        if (libertyWeapon)
                        {
                            libertyWeapon.OnWeaponFire();
                        }
                    }
                    break;
                
                case AnimationState.RPGReloadStart:
                    {
                        // 开始装弹
                        // 显示弹夹
                        _aiEntity.battleAgent.ShowMagazine();
                    }
                    break;
                
                case AnimationState.RPGReload:
                    {
                        // 结束装弹
                        // 装弹处理
                        _aiEntity.battleAgent.GunReload();
                        // 隐藏弹夹
                        _aiEntity.battleAgent.HideMagazine();
                        // 手动结束换弹状态
                        _aiEntity.viewer.animator.SetBool("Reload", false);
                        
                        // 重新显示武器上隐藏的部分
                        LibertyWeapon libertyWeapon = _aiEntity.weapon;
                        if (libertyWeapon)
                        {
                            libertyWeapon.OnWeaponReload();
                        }
                    }
                    break;
            }
        }

        /// <summary>
        /// 音效事件
        /// </summary>
        /// <param name="evt"></param>
        void OnSoundEvent(AnimationEvent evt)
        {
            string eventName = evt.stringParameter;

            AudioManager.PlaySound(eventName, _aiEntity.viewer.animator.gameObject);
        }
        
#endregion

#region IK

        /// <summary>
        /// 设置LookAt是否支持空闲时看向玩家
        /// </summary>
        /// <param name="support"></param>
        public void SetSupportLookAtPlayer(bool support)
        {
            _IKAgent.SetSupportLookAtPlayer(support);   
        }

        /// <summary>
        /// 设置LookAt的目标
        /// </summary>
        /// <param name="target"></param>
        public void SetLookAtTarget(IKAgent.LookAtType lookAtType, Transform target)
        {
            _IKAgent.SetLookAtTarget(lookAtType, target);
        }

        /// <summary>
        /// 清除LookAt目标，并重置LookAt类型
        /// </summary>
        public void ClearLookAtTarget()
        {
            _IKAgent.ClearLookAtTarget();
        }

        /// <summary>
        /// LookAt淡入
        /// </summary>
        public void LookAtIKFadeIn()
        {
            _IKAgent.LookAtIKFadeIn();
        }

        /// <summary>
        /// LookAt淡出
        /// </summary>
        public void LookAtIKFadeOut()
        {
            _IKAgent.LookAtIKFadeOut();
        }

        /// <summary>
        /// 获取LookAt头部骨骼Transform
        /// </summary>
        /// <returns></returns>
        public Transform GetLookAtIKHead()
        {
            return _IKAgent.GetLookAtIKHead();
        }

        /// <summary>
        /// 设置瞄准IK的目标
        /// </summary>
        /// <param name="ikType"></param>
        /// <param name="target"></param>
        public void SetAimTarget(WeaponIKType ikType, Transform target)
        {
            switch (ikType)
            {
                case WeaponIKType.HandGunAimIK:
                    {
                        _IKAgent.SetHandGunAimTarget(target);
                    }
                    break;
            }
        }

        /// <summary>
        /// 设置瞄准IK作用的Transform节点
        /// </summary>
        /// <param name="aimTransform"></param>
        public void SetAimTransform(WeaponIKType ikType, Transform aimTransform)
        {
            switch (ikType)
            {
                case WeaponIKType.HandGunAimIK:
                    {
                        _IKAgent.SetHandGunAimTransform(aimTransform);
                    }
                    break;
            }
        }

#endregion

        void AfterHandGunIKUpdate()
        {
            if (!_rangeWeaponFirePositionDirty)
            {
                return;
            }

            if (_aiEntity.weapon)
            {
                _rangeWeaponFirePositionDirty = false;
                _aiData.rangedWeaponFirePosition = _aiEntity.weapon.transform.position;
            }
        }

        void Update()
        {
            if (_aiEntity == null || _aiData == null)
            {
                return;
            }
            BattleAgent battleAgent = _aiEntity.battleAgent;
            
            // 禁用动画输入倒计时
            if(_disableOperateTimestamp>0 && Time.time>=_disableOperateTimestamp)
            {
                SetDisableOperate(false);
            }

            // WeaponSystem weaponSystem = _aiEntity.WeaponSystem;
            // // 判断是否要切换作战状态 (选择的状态 和 当前作战状态 不一致)
            // if (CanDoNextAnimation() &&( weaponSystem.IsEmpty()
            //     || (weaponSystem.CurrentWeapon.Config !=null && weaponSystem.CurrentWeapon.Config.weapon_id != weaponSystem.SelectedWeaponId))
            //     || (weaponSystem.CurrentWeapon.Config !=null && weaponSystem.CurrentWeapon.Config.battle_state!=(int)_aiData.battleState))
            // {
            //     switch (_aiData.animationState)
            //     {
            //         case AnimationState.Undefine:
            //         case AnimationState.Idle:
            //         case AnimationState.Locomotion:
            //         case AnimationState.Sprinting:
            //         case AnimationState.DodgeSprint:
            //         case AnimationState.Dodge:
            //         {
            //             // 替换装备
            //             _aiEntity.battleAgent.ChangeWeapon(weaponSystem.SelectedWeaponId);
            //             BattleState battleState = BattleState.None;
            //             // 切换状态
            //             PbWeaponDataConfig weaponDataConfig = LibertyConfigManager.GetInstance().GetWeaponDataConfig(weaponSystem.SelectedWeaponId);
            //             if (weaponDataConfig != null)
            //             {
            //                 battleState = (BattleState)weaponDataConfig.battle_state;
            //             }
            //             EnterBattleState(battleState);
            //             EnterAimingState(AimingState.None);
            //         }
            //         break;
            //     }
            // }
            
            // 受击起身动作
            switch (_aiData.animationState)
            {
                case AnimationState.OnHitDown:
                    {
                        if (_aiData.isAlive && Time.time>=onHitGetUpTimestamp)
                        {
                            PlayHurtGetUp();
                        }
                    }
                    break;
            }

            // 处理不同战斗状态内的细节需求
            if (_aiData.uniqueId == LibertyAIUtils.PLAYER_AI_ID)
            {
                // 玩家
                switch (_aiData.battleState)
                {
                    case BattleState.None:
                        {
                        }
                        break;

                    case BattleState.Boxing:
                        {
                            // 处理回到idle状态
                            if (_keepBattleDuration > 0)
                            {
                                if (Time.time >= _lastBattleTimestamp + _keepBattleDuration)
                                {
                                    // EnterBattleState(BattleState.None);
                                    EnterAimingState(AimingState.None);
                                }
                            }
                        }
                        break;

                    case BattleState.HandGun:
                    case BattleState.AutoRifle:
                    case BattleState.RPG:
                        {
                            // 处理回到idle状态
                            if (_keepBattleDuration > 0)
                            {
                                if (Time.time >= _lastBattleTimestamp + _keepBattleDuration)
                                {
                                    // EnterBattleState(BattleState.None);
                                    EnterAimingState(AimingState.None);
                                }
                            }
                            
                            // 处理武器换弹
                            if (battleAgent.GetRestBulletCount()<=0 && battleAgent.IsRestBulletCountEnough() && !battleAgent.IsRestBulletCountFull() && CanDoNextAnimation())
                            {
                                PlayHandGunReload();
                            }
                        }
                        break;

                    case BattleState.Bomb:
                        {
                            // 处理武器换弹
                            if (battleAgent.GetRestBulletCount()<=0 && battleAgent.IsRestBulletCountEnough() && !battleAgent.IsRestBulletCountFull() && CanDoNextAnimation())
                            {
                                PlayGrenadeReload();
                            }
                        }
                        break;
                }
            }
            
            
            // 根据瞄准状态判断是否启用瞄准IK
            switch (_aiData.aimingState)
            {
                case AimingState.NormalAim:
                    {
                        if (_aiData.animationStateUseIK)
                        {
                            // 开启瞄准IK   
                            _IKAgent.HandGunAimIKFadeIn(); 
                        }
                        else
                        {
                            // 关闭瞄准IK   
                            _IKAgent.SetHandGunEnabled(false);
                        }
                    }
                    break;

                case AimingState.ReloadAim:
                    {
                        // // 关闭瞄准IK   
                        // _IKAgent.SetHandGunEnabled(false);
                        
                        if (reloadFinishTimestamp>0 && Time.time>=reloadFinishTimestamp)
                        {
                            EnterAimingState(AimingState.NormalAim);
                        }
                    }
                    break;

                case AimingState.Grenade:
                default:
                    {
                        // 关闭瞄准LookAtIK
                        if (_IKAgent.GetLookAtType() == IKAgent.LookAtType.Aim)
                        {
                            _IKAgent.ClearLookAtTarget();
                            _IKAgent.LookAtIKFadeOut();
                        }
                        // 关闭瞄准IK
                        _IKAgent.HandGunAimIKFadeOut();
                    }
                    break;
            }
        }

#if UNITY_EDITOR
        private void OnDrawGizmos()
        {
            if (_aiData == null)
            {
                return;
            }

            switch (_aiData.battleState)
            {
                case BattleState.Boxing:
                    {
                        // 显示扇区
                        PbBoxingDataConfig boxingDataConfig = LibertyConfigManager.GetInstance().GetBoxingDataConfig(_aiData.uniqueId, GetAnimationState());
                        if (boxingDataConfig != null)
                        {
                            float radius = boxingDataConfig.range_parameter_1;  // 扇形半径
                            float degree = boxingDataConfig.range_parameter_2;  // 扇形角度
                            float theta = -degree / 2f;
                            float halfTheta = theta / 2f * Mathf.Deg2Rad;
                            float sinHalfTheta = Mathf.Sin(halfTheta);
                            float cosHalfTheta = Mathf.Cos(halfTheta);
                            UnityEditor.Handles.DrawSolidArc(transform.position, Vector3.up, new Quaternion(0,sinHalfTheta, 0, cosHalfTheta) * transform.forward, degree, radius);
                        }
                            
                    }
                    break;

                case BattleState.HandGun:
                case BattleState.AutoRifle:
                case BattleState.RPG:
                    {
                        // 显示射线
                        PbWeaponDataConfig weaponDataConfig = _aiEntity.WeaponSystem.HandleWeapon.Config;
                        Transform weapon = _aiEntity.weapon.transform;
                        if (weaponDataConfig!=null && weapon)
                        {
                            // 武器开火位置
                            Vector3 weaponPosition = _aiData.rangedWeaponFirePosition;
                            // 准星位置
                            Vector3 aimPosition = _aiData.aimPosition;
                            // 射线起点
                            Vector3 raycastOrigin = weaponPosition;
                            // 如果是玩家，并且是自由射击的情况下，射线从camera处发射
                            if (_aiData.uniqueId == LibertyAIUtils.PLAYER_AI_ID && LibertyAIUtils.PlayerController.playerLockTargetHelper.UseFreeAim)
                            {
                                raycastOrigin = LibertyAIUtils.PlayerController.cameraController.playerCamera.transform.position;
                                // 向前移动相机跟角色的偏移值，防止自由瞄准时，射线击中人物后背
                                Vector3 direction = (aimPosition - raycastOrigin).normalized;
                                raycastOrigin += direction * Vector3.Dot(direction, (weaponPosition - raycastOrigin));
                            }
                            Color oldColor = UnityEditor.Handles.color;
                            UnityEditor.Handles.color = Color.green;
                            UnityEditor.Handles.DrawLine(raycastOrigin, raycastOrigin+(aimPosition-raycastOrigin).normalized*weaponDataConfig.range);
                            UnityEditor.Handles.color = oldColor;
                        }
                    }
                    break;
            }
                
        }
#endif


    }
}